﻿#include "GuardianRock.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/ServerPackets.h"
#include "../../Settings.h"
#include "../../../Shared/Data/Stat.h"
#include "../DelayedAction.h"

using namespace Server::MirDatabase;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    bool GuardianRock::getCanMove() const
    {
        return false;
    }

    GuardianRock::GuardianRock(MonsterInfo *info) : MonsterObject(info)
    {
        setDirection(MirDirection::Up);
    }

    void GuardianRock::Turn(MirDirection dir)
    {
    }

    bool GuardianRock::Walk(MirDirection dir)
    {
        return false;
    }

    void GuardianRock::ProcessRoam()
    {
    }

    bool GuardianRock::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), Info->ViewRange);
    }

    void GuardianRock::CompleteAttack(std::vector<std::any> &data)
    {
        if (getTarget() == nullptr)
        {
            return;
        }
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        if (getTarget()->getCurrentMap() != getCurrentMap() || getTarget()->Node == nullptr)
        {
            return;
        }

        S::ObjectRangeAttack *tempVar = new S::ObjectRangeAttack();
        tempVar->ObjectID = ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        tempVar->TargetID = getTarget()->ObjectID;
        Broadcast(tempVar);
        PullAttack();

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void GuardianRock::PullAttack()
    {
        MirDirection pushdir = Functions::DirectionFromPoint(getTarget()->getCurrentLocation(), getCurrentLocation());
        if (getEnvir()->Random->Next(Settings::MagicResistWeight) < getTarget()->Stats[Stat::MagicResist])
        {
            return;
        }
        int distance = Functions::MaxDistance(getTarget()->getCurrentLocation(), getCurrentLocation()) - 1;
        if (distance <= 0)
        {
            return;
        }
        if (distance > 4)
        {
            distance = 4;
        }

        getTarget()->Pushed(this, pushdir, distance);
    }

    void GuardianRock::ProcessTarget()
    {
        if (getTarget() == nullptr)
        {
            return;
        }
        if (!Active)
        {
            return;
        }
        if (InAttackRange() && getCanAttack())
        {
            DelayedAction tempVar(DelayedType::Damage, getEnvir()->getTime() + 500);
            ActionList.push_back(&tempVar);
            return;
        }

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }
    }

    int GuardianRock::Attacked(MonsterObject *attacker, int damage, DefenceType type)
    {
        return 0;
    }

    int GuardianRock::Attacked(PlayerObject *attacker, int damage, DefenceType type, bool damageWeapon)
    {
        return 0;
    }

    int GuardianRock::Struck(int damage, DefenceType type)
    {
        return 0;
    }

    void GuardianRock::ChangeHP(int amount)
    {
        //make it immune to green poison
    }
}
